﻿using Comm;
using Kinlo.Drv.Drv.RJShortCircuitEnum;
using Kinlo.Drv.Drv.RJShortCirrcuitResult;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kinlo.Drv.DrvBases
{
    public abstract class RJShortCircuitBase<Result1, Result2> where Result1 : Result where Result2 : Result
    {
        /// <summary>
        /// 连接
        /// </summary>
        /// <returns></returns>
        public abstract Result Connect();
        /// <summary>
        /// 断开连接
        /// </summary>
        /// <returns></returns>
        public abstract Result DisConnect();
        /// <summary>
        /// 启动测试
        /// </summary>
        /// <param name="localID"></param>
        /// <returns></returns>
        public abstract Result StartTest(byte localID);
        /// <summary>
        /// 获取测试数据和结果
        /// </summary>
        /// <param name="localID"></param>
        /// <returns></returns>
        public abstract Result1 GetTestDataAndResult(byte localID);
        /// <summary>
        /// 获取测试结果
        /// </summary>
        /// <param name="localID"></param>
        /// <returns></returns>
        public abstract Result2 GetTestResult(byte localID);

        /// <summary>
        /// 返回结果
        /// </summary>
        /// <param name="b">结果字节</param>
        /// <returns>对单一字节进行分析并返回的结果，返回null标识无测试结果,true表示OK,false表示NG</returns>
        protected virtual bool? GetResult(byte b)
        {
            switch (b)
            {
                case 0:
                    return null;
                case 1:
                    return true;
                case 0xFF:
                default:
                    return false;
            }
        }

        /// <summary>
        /// 获取校验和
        /// </summary>
        /// <param name="cn">发送报文</param>
        protected virtual void GetCheckNumber(ref List<byte> cn)
        {
            int val = 0;
            try
            {
                for (int i = 0; i < cn.Count; i++)
                {
                    if (i == 0 || i == cn.Count - 1)
                    {
                        continue;
                    }
                    val += cn[i];
                }
                cn.Insert(cn.Count - 1, byte.Parse(int.Parse(val.ToString("X").Substring(val.ToString().Length - 2), System.Globalization.NumberStyles.HexNumber).ToString()));
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 返回结果
        /// </summary>
        /// <param name="data">返回报文</param>
        /// <returns>对整体报文进行分析并返回的结果对象</returns>
        protected virtual Result GetResult(byte[] data)
        {
            try
            {
                byte val = data[data.Length - 1];
                Result result = new Result();
                switch (val)
                {
                    case 0x01:
                        result.ErroMessage = "校验和错误";
                        break;
                    case 0x02:
                        result.ErroMessage = "命令类型错误";
                        break;
                    case 0x03:
                        result.ErroMessage = "命令字错误";
                        break;
                    case 0x04:
                        result.ErroMessage = "状态不符";
                        break;
                    case 0x05:
                        result.ErroMessage = "参数错误(参数无效或个数不符)";
                        break;
                    default:
                        result.IsSuccess = true;
                        result.ErroMessage = "";
                        break;
                }
                result.Data = data;
                return result;
            }
            catch (Exception ex)
            {
                return new Result { Data = data, ErroMessage = ex.Message, IsSuccess = false };
            }
        }
    }
}
